/*****************************************************************************
 * kal_cmsis_os2_thread.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_DEBUG
#define LOG_MODULE    "kal.thread"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/
#define KAL_THREAD_NAME_MAX 32
/*****************************************************************************
 * Private Types
 ****************************************************************************/

typedef struct {
    osThreadId       hdl;
    plum_s8          name[KAL_THREAD_NAME_MAX];
    struct list_head list;
} kal_cmsis_os2_thr_t;
/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
kal_cmsis_os2_thr_t *kal_cmsis_os_thr_register(osThreadId_t hdl, plum_s8 *name)
{
    kal_cmsis_os2_thr_t *node = plum_null;

    do {
        if (!thr_head.list.next) {
        }

        node = plum_kal_mem_malloc(sizeof(kal_cmsis_os2_thr_t));
        if (!node) {
            LOG_E("plum_kal_mem_malloc err");
            break;
        }

        memset(node, 0, sizeof(kal_cmsis_os2_thr_t));
        snprintf(node->name, sizeof(node->name), "%s", name);
        node->hdl = hdl;

        list_add();
    } while (0);

    return (node);
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_kal_thread_create(PLUM_CONST plum_s8 *name,
                                plum_void (*task)(plum_void *arg),
                                plum_void *arg, plum_kal_thread_priority_t pri,
                                plum_u32 stack_sz)
{
    plum_s32 rc = PLUM_ECODE_OK;

    osThreadAttr_t attr = {.name = name;
    .stack_size         = stack_sz;
    .priority           = pri;
};
osThreadId_t osHdl = osThreadNew(task, arg, &attr);
if (osHdl == plum_void) {
    kal_cmsis_os_thr_register(osHdl, name);
    rc = PLUM_ECODE_UNK;
}

return (rc);
}

/*****************************************************************************
 * Cli Functions
 ****************************************************************************/

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
